<template>
<div>
    <Input v-model="filter.name">
        <Button slot="append" icon="ios-search" @click="search"></Button>
    </Input>
    <Button type="primary" @click="addData" style="margin:5px;">添加用户</Button>
    <Button type="error"  @click="removeData" style="margin:5px;">删除选中</Button>    
    <Table :columns="columns1" :data="list" @on-selection-change="removeData"></Table>
    <Page :total="filter.total" show-elevator show-total size="small" show-sizer :page-size="1" @on-page-size-change="onPageSizeChange"  @on-change="changePage"></Page>    
    <Modal
        v-model="modal1"
        title="普通的Modal对话框标题"
        @on-ok="ok"
        @on-cancel="cancel">
                                                                <!-- :rules="ruleValidate" -->       
        <Form ref="formValidate" :model="formValidate" :label-width="80">
            <FormItem label="姓名" prop="name">
                <Input v-model="formValidate.name" placeholder="请输入姓名"></Input>
            </FormItem>
            <FormItem label="邮箱" prop="email">
                <Input v-model="formValidate.email" placeholder="请输入邮箱"></Input>
            </FormItem>
            <FormItem label="年龄" prop="age">
                <Input v-model="formValidate.age" placeholder="请输入年龄"></Input>
            </FormItem>
            <FormItem label="选择日期">
                <Row>
                    <Col span="11">
                        <FormItem prop="date">
                            <DatePicker type="date" placeholder="选择日期" v-model="formValidate.date"></DatePicker>
                        </FormItem>
                    </Col>
                    <Col span="2" style="text-align: center">-</Col>
                    <Col span="11">
                        <FormItem prop="time">
                            <TimePicker type="time" placeholder="选择时间" v-model="formValidate.time"></TimePicker>
                        </FormItem>
                    </Col>
                </Row>
            </FormItem>
           
            <FormItem>
                <Button type="primary" @click="handleSubmit('formValidate')">提交</Button>
                <Button type="ghost" @click="handleReset('formValidate')" style="margin-left: 8px">重置</Button>
            </FormItem>
        </Form>

    </Modal>

</div>
</template>
<script>

    import Base from '../../common/Base.js'

    export default {
        mixins:[Base],
        data () {
            return {
                module:'users',
                columns1: [
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center'
                    },
                    {
                        title: '姓名',
                        key: 'name',
                    },
                    {
                        title:'邮箱',
                        key:'email'
                    },
                    {
                        title:'年龄',
                        key:'age'
                    },
                    {
                        title:'出生日期',
                        key:'birthday'
                    },
                    {
                        title: '操作',
                        key: 'action',
                        width: 150,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: () => {
                                            this.editData(params.row._id)
                                        }
                                    }
                                }, '编辑'),
                                h('Button', {
                                    props: {
                                        type: 'error',
                                        size: 'small'
                                    },
                                    on: {
                                        click: () => {
                                            this.removeData(params.row._id)
                                            this.getData();
                                        }
                                    }
                                }, '删除')
                            ]);
                        }
                    }
                ],
                
                formValidate: {
                    name: '',
                    email: '',
                    age:'',
                    date: '',
                    time: '',
                },
                ruleValidate: {
                    name: [
                        { required: true, message: '姓名不能为空', trigger: 'blur' }
                    ],
                    email: [
                        { required: true, message: '邮箱不能为空', trigger: 'blur' },
                        { type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
                    ],
                    date: [
                        { required: true, type: 'date', message: '请选择日期', trigger: 'change' }
                    ],
                    time: [
                        { required: true, type: 'date', message: '请选择时间', trigger: 'change' }
                    ]
                }
            }
        },
        methods:{
            handleSubmit (name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        //console.log(this.formValidate)
                        //console.log(this.formValidate._id);
                        var id=this.formValidate._id;
                        //console.log(id);

                        if(id){
                            this.$http({
                            method:'POST',
                            url:'http://localhost:3000/users/data/'+id,
                            data:this.formValidate
                            }).then(res=>{
                                this.getData();
                            })
                        }

                        this.$http({
                            method:'POST',
                            url:'http://localhost:3000/users/data',
                            data:this.formValidate
                        }).then(res=>{
                            this.getData();
                        })
                        this.$Message.success('提交成功!');
                    } else {
                        this.$Message.error('表单验证失败!');
                    }
                })
                this.getData();
            },
            handleReset (name) {
                this.$refs[name].resetFields();
            },
            ok () {
                this.$Message.info('点击了确定');
            },
            cancel () {
                this.$Message.info('点击了取消');
            }
        }
    }
</script>